<?php
namespace  Partner\Controller;
use Utuo\BackgroundController;

/**
 * 合作商管理客户角色类
 * @author song
 */
class RoleController extends BackgroundController {
	
	private $status=array(
		'1'=>'开启',
		'0'=>'关闭'	
	);

	private $tableNames; // 所有权限表的模型名 
	
	public function _initialize() {
		$this->setControllerTitle('角色管理');
		$this->setActionTitle(array(
				'index' => '角色列表',
				'add' => '添加角色',
				'modify' => '修改角色',
				'recycle' => '回收站',
				'access'=>'配置权限'
				
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
		$rbacMy = C('RBAC_MY');
		$this->tableNames = \Utuo\Rbac::getAuthModelNames( array('role' => $rbacMy['RBAC_ROLE_TABLE'], 'user' => $rbacMy['RBAC_USER_TABLE'], 'access' => $rbacMy['RBAC_ACCESS_TABLE'], 'node' => $rbacMy['RBAC_NODE_TABLE']) );
	}   
	
	public function getLists($is_deleted) {
		$condition = array(
			'pid' => $this->aid,
			'is_deleted' => $is_deleted,
	    );
		$role = M($this->tableNames['role'][1])->where($condition)->select();
		$this->assign("role",$role);
		$this->display();
	}
	
	/**
	 * 添加客户角色
	 * @see \Utuo\BackgroundController::addAction()
	 */
	public function addAction() {
		$role['radio'] = \Utuo\Form::getRadio("status", $this->status, 1);
		$this->assign('role',$role);
		$this->display('save');
	}
	
	/**
	 * 修改客户角色
	 * @see \Utuo\BackgroundController::addAction()
	 */
	public function modifyAction($id) {
		$role = M($this->tableNames['role'][1])->getById($id);
		$role['radio']=\Utuo\Form::getRadio("status", $this->status, $role['status']);
		$this->assign('role', $role);
		$this->display('save');
	}
	
	/**
	 * 为客户角色分配权限
	 * @see \Utuo\BackgroundController::addAction()
	 */
	public function accessAction( $rid ) {
		/*获得角色已有的权限*/ 
		$nodeIds = M($this->tableNames['access'][1])->where('role_id='.$rid)->getField('node_id',true);
		$nodeIds = implode($nodeIds,',');
		/*已分配给合作商可分配客户的节点*/
		$m = M('ParnterNode p');
		$nodes = $m->join('__NODE__ n on p.node_id=n.id')->where('n.status=1 and n.is_deleted=0 and p.partner_id='.$this->aid)->select();
		// 获得客户的模块
		$rbacModule=C('RBAC_MODULE');
		$node = M('node')->where("name='".$rbacModule['MODULE_MY']."' and pid=0 and level=1")->find();
		$nodes = makeListLevel($nodes, $node['id'], 'pid');
	    $node['child'] = $nodes;
	    $this->assign("rid",$rid);
        $this->assign("nodeIds",$nodeIds);
		$this->assign('nodes',$node);
		$this->display();
	}
	
	/**
	 * 保存客户角色分配权限
	 */
	public function setAccessAction() {
		$role_id = I('role_id');
		M($this->tableNames['access'][1])->where('role_id='.$role_id)->delete();
		$array = I('access');
		$temparray = array();
		foreach ($array as $v) {
			$temp = explode('-', $v);
			$temparray[]=array(
				'role_id'=>$role_id,
				'node_id'=>$temp[0],
				'level'=>$temp[1],
			);
		}
		!$temparray and $this->ajaxSuccess('配置成功');
		M($this->tableNames['access'][1])->addAll($temparray)!==false?$this->ajaxSuccess('配置成功'):$this->ajaxError('配置失败');
	}
	
	/**
	 * 保存客户角色信息
	 * @see \Utuo\BackgroundController::saveAction()
	 */
	public function saveAction() {
		$this->submitPost($this->tableNames['role'][1]);
	}
	
	// 提交角色
	public function submitPost( $model ) {
		$model = is_object($model) ? $model : D($model);
		if ($model->isCreated or $model->create()) {
			$model->cid = 0; // 合作商管理客户的公共角色cid为0
			if (empty($_REQUEST['id'])) {
				unset( $model->id );
				if ($model->add()) {
					$this->ajaxSuccess("添加成功！");
				} else {
					$this->ajaxError("添加失败！".$model->getError());
				}
			} else {
				if (false !== $model->save()) {
					$this->ajaxSuccess("修改成功！");
				} else {
					$this->ajaxError("修改失败！");
				}
			}
		} else {
			$this->ajaxError($model->getError());
		}
	}
	
}